<?php
/**
 * 首页
 *
 * @version $Id$
 */
class ControllerSite extends Controller
{
	/**
	 * 入口
	 */
	public function actionHome()
	{
		$hour = intval(date('H'));
		$time = $hour > 10 ? time() : strtotime("-1 day");
		$date = date('Y_m_d', $time);

		///
		$model = App::m('Item');
		$list = $model->getList($date);
		$data = $this->_groupCol($list);
		list($classes, $list) = $data;
		// pr($data);exit();

		///
		$this->display('home');
		$this->assign('list', $list);
		$this->assign('classes', $classes);
	}

	/**
	 * 跳转
	 */
	public function actionGo()
	{
		$url = "http://s.click.taobao.com/t_9?p=mm_26200035_0_0&l=";
		$url .= urlencode("http://item.tmall.com/item.htm?id=") . $_GET['id'];

		///
		// echo $url;
		header("Location:". $url);
	}

	/**
	 * 404
	 */
	public function action404()
	{
		echo "404";
	}

	/**
	 * debug
	 */
	public function actionDebug()
	{
		echo "debug";
	}

	/**
	 * 分组
	 */
	private function _groupCol($list, $base = 4)
	{
		$i = 0;
		$data = array();
		$classes = array();
		$counts  = array();
		if (is_array($list) && ! empty($list)) {
			foreach ($list as $v) {
				if (! in_array($v['class'], $classes))
					$classes[] = $v['class'];

				///
				$key = array_search($v['class'], $classes);
				$counts[$key]++;
				$v['image'] = base64_encode($v['image']);
				$v['class'] = $key;

				/*
				$k = $i++ % $base;
				$data[$k][] = $v;
				*/
				$data[] = $v;
			}
		}
		foreach ($classes as $key => $v)
			$classes[$key] = array(
				$v, $counts[$key]
			);

		///
		return array(
			$classes,
			$data
		);
	}
}
